package com.uchengwang.ui.base_ui.listener;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.jpush.JpushSetAlisTagUtil;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.login.ui.LoginActivity;
import com.uchengwang.ui.utils.ActivityUtils;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.SPUtils;

import io.rong.imlib.RongIMClient;

/**
 * Created by U城网 on 2017/8/16.
 */

public class MyConnectionListener implements RongIMClient.ConnectionStatusListener {
    private Activity activity;
    public MyConnectionListener(Activity activity){
        this.activity = activity;
    }

    @Override
    public void onChanged(ConnectionStatus connectionStatus) {
        switch (connectionStatus){
            case CONNECTED://连接成功。
//                UCommonUtil.getUtil().showToast(activity, "连接成功");
                Log.d("ryjjf", "监听融云连接成功");
                break;
            case DISCONNECTED://断开连接。
                //无法访问到服务器
                Log.d("ryjjf", "监听融云断开连接");
//                if (NetwotkUtil.isConnectInternet()){
//                    //连接不到聊天服务器
//                    if (UCommonUtil.getUtil().isLogin()){
//                        RongChatManager.getUtils().getTokenAdnLink(activity, null);
//                    }
//                }else {
//                    //网络问题
//                    UCommonUtil.getUtil().showToast(activity, "网络连接异常");
//                }
                break;
            case CONNECTING://连接中。

                break;
            case NETWORK_UNAVAILABLE://网络不可用。
                Log.d("ryjjf", "网络连接异常");
                break;
            case KICKED_OFFLINE_BY_OTHER_CLIENT://用户账户在其他设备登录，本机会被踢掉线
                Log.d("ryjjf", "账号在另外设备登录");
                // 显示帐号在其他设备登录
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.getUtils().showDialogSingle(activity, false, "", "你的账号在别的设备登录, 请重新登录验证账号, 以免影响正常使用", "确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                DialogUtils.getUtils().cancel();

                                UChengApplication.TOKEN = "";
                                UChengApplication.uid = 0;
                                UChengApplication.nickName = "";
                                UChengApplication.uniId = 0;
                                UChengApplication.myUniId = 0;
                                UChengApplication.schoolName = "";
                                UChengApplication.mySchoolName = "";
                                UChengApplication.userAvatar = "";
                                UChengApplication.userRole = 0;

                                SPUtils.getPreferences(activity).setAccountPw("");
                                SPUtils.getPreferences(activity).setToken("");
                                SPUtils.getPreferences(activity).putInt(BaseConfig.USER_ROLE, 0);
                                SPUtils.getPreferences(activity).setRyToken("");
                                JpushSetAlisTagUtil.getUtil().setNullAlis(activity);
                                SPUtils.getPreferences(activity).setLinkMainToken("");
                                SPUtils.getPreferences(activity).putLong(BaseConfig.UNI_ID, 0);

                                Intent intent = new Intent(activity, LoginActivity.class);
                                intent.putExtra(BaseConfig.IS_TO_MAIN, true);
                                activity.startActivity(intent);
                                ActivityUtils.getUtils().overLoginActivity();
                            }
                        });
                    }
                });

                break;
        }
    }
}
